package ccgl.com.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import ccgl.com.dao.common.BaseDao;
import ccgl.com.factory.ConnectionFactory;
import ccgl.com.model.MDd;
import ccgl.com.model.MDdxx;

/**
 * 商品管理Dao
 * 
 * @author Administrator
 * 
 */
public class DdglDao extends BaseDao<MDd> {

	/**
	 * 添加订单信息
	 * 
	 * @param dd
	 * @return
	 * @throws SQLException
	 */
	public boolean addDdxx(MDd dd) throws SQLException {
		String sql = "insert into ddb(id,ddh,khxm,khdh,xsr,ddzj,userid,ddsj,gxsj) values(?,?,?,?,?,?,?,?,?)";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, dd.getPropertiesArray()) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	/**
	 * 更新订单信息
	 * 
	 * @param dd
	 * @return
	 * @throws SQLException
	 */
	public boolean updateDdxx(MDd dd) throws SQLException {
		String sql = "update ddb set  ddh=?,khxm=?,khdh=?,xsr=?,ddzj=?,ddsj=?,gxsj=? where id='"
				+ dd.getId() + "'";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, dd.getPropertiesUpdateArray()) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	public boolean exists(String ddh, String userid) throws SQLException {
		String sql = "select count(*) from ddb where ddh='" + ddh
				+ "' and userid='" + userid + "'";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.getCount(sql, con) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	public Map getDdList(int star, int end, String userid) throws SQLException {
		String sql = "SELECT * FROM ddb where userid='" + userid + "' LIMIT "
				+ star + "," + end;
		String countSql = "select count(*) from ddb where userid='" + userid
				+ "'";
		return getDdList(sql, countSql);
	}

	public Map getDdList(String sql, String countSql) throws SQLException {
		Map map = new HashMap();
		Connection con = ConnectionFactory.getConnection();
		try {
			map.put("Rows", super.queryForList(sql, con));
			map.put("Total", super.getCount(countSql, con));
		} finally {
			con.close();
		}
		return map;
	}

	public boolean deleteDdxxs(String idStr) throws SQLException {
		String sql = "delete from ddb  where id in (" + idStr + ")";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	public boolean updateDdxxxx(MDdxx ddxx) throws SQLException {
		String sql = "update ddxx set xh=?, sl=?, dj=?, xslx=? where id='"
				+ ddxx.getId() + "'";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, ddxx.getPropertiesUpdateArray()) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	public boolean saveDdxxxx(MDdxx ddxx) throws SQLException {
		String sql = "insert into ddxx(id, ddid, ddh, xh, sl, dj, xslx, gxsj) values(?,?,?,?,?,?,?,?)";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, ddxx.getPropertiesArray()) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

	public boolean deleteDdxxxx(String idStr) throws SQLException {
		String sql = "delete from ddxx  where id in (" + idStr + ")";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}
}
